<template>
  <div class="dashboard-container">
    <div class="app-container">
      <!-- 添加 -->
      <el-dialog :title="title" :visible.sync="dialogFormVisible">
        <el-form ref="addRef" :model="addform">
          <el-form-item label="角色名称">
            <el-input v-model="addform.name" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="角色描述">
            <el-input
              v-model="addform.description"
              autocomplete="off"
            ></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="postRole()">确 定</el-button>
        </div>
      </el-dialog>
      <!-- 给角色分配权限 -->
      <el-dialog title="分配权限" :visible.sync="setDialogFormVisible">
        <el-tree
          ref="treeRef"
          :data="treeData"
          :props="{ label: 'name' }"
          show-checkbox
          default-expand-all
          :check-strictly="true"
          :default-checked-keys="checkedNode"
          node-key="id"
          :get-checked-nodes="selectedNode"
          @check-change="handleCheckChange"
        >
        </el-tree>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="postSetRole"
            >确 定</el-button
          >
        </div>
      </el-dialog>
      <el-card>
        <el-tabs v-model="activeName">
          <el-tab-pane label="角色信息" name="roleInfo">
            <el-button type="primary" size="medium" @click="showDialog()"
              >添加角色</el-button
            >
            <el-table :data="roleList" border style="width: 100%">
              <el-table-column type="index" width="50"></el-table-column>
              <el-table-column prop="name" label="角色名称" width="180">
              </el-table-column>
              <el-table-column prop="description" label="描述">
              </el-table-column>
              <el-table-column prop="companyId" label="操作">
                <template slot-scope="scope">
                  <el-button
                    type="primary"
                    plain
                    size="mini"
                    @click="showDialog(scope.row.id)"
                    >编辑角色</el-button
                  >
                  <el-button
                    type="warning"
                    plain
                    size="mini"
                    @click="showSetDialog(scope.row.id)"
                    >分配角色</el-button
                  >
                  <el-button
                    type="danger"
                    plain
                    size="mini"
                    @click="deleteRole(scope.row.id)"
                    >删除角色</el-button
                  >
                </template>
              </el-table-column>
            </el-table>
          </el-tab-pane>
          <el-tab-pane label="公司信息" name="companyInfo">
            <el-alert
              title="对公司名称、公司地址、营业执照、公司地区的更新。应谨慎修改"
              type="info"
              :closable="false"
            ></el-alert>
            <el-form ref="form" :model="companyInfo" label-width="80px">
              <el-form-item label="企业名称">
                <el-input v-model="companyInfo.name"></el-input>
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input v-model="companyInfo.Address"></el-input>
              </el-form-item>
              <el-form-item label="电话">
                <el-input v-model="companyInfo.Phone"></el-input>
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input v-model="companyInfo.mailbox"></el-input>
              </el-form-item>
              <el-form-item label="备注">
                <el-input
                  v-model="companyInfo.remarks"
                  type="textarea"
                  :rows="3"
                  style="width:400px"
                ></el-input>
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
  </div>
</template>

<script>
import { getRole } from "@/api/setting";
import { getPermissionList } from "@/api/permission";
import { tranListToTreeData } from "@/utils/toTreedata";
import {
  addRole,
  delRole,
  updateRole,
  getRoleDetail,
  companyInfo,
  setRole
} from "@/api/setting";
export default {
  data() {
    return {
      selectedRole: [],
      title: "",
      dialogFormVisible: false,
      setDialogFormVisible: false,
      addform: {
        id: 0,
        name: "",
        description: ""
      },
      activeName: "roleInfo",
      roleList: [],
      pageInfo: {
        page: 1,
        pagesize: 10,
        total: 0
      },
      companyInfo: {},
      treeData: [],
      // 初始有的权限
      checkedNode: [],
      // 选择后的权限
      selectedNode: [],
      // 当前要分配权限的角色
      setId: null
    };
  },
  created() {
    this.getRoleList(); // 获取角色列表
    this.getCompanyInfo();
  },
  methods: {
    async getRoleList() {
      const res = await getRole(this.pageInfo);
      console.log(res);
      this.pageInfo.total = res.total;
      this.roleList = res.rows;
      console.log(res);
    },
    async postRole() {
      this.$refs.addRef.validate(async valid => {
        if (valid) {
          console.log(this.addform);
          if (this.addform.id) {
            // 编辑
            console.log(2);
            await updateRole(this.addform);
          } else {
            // 添加
            console.log(1);
            await addRole(this.addform);
          }
          this.addform = {};
          this.dialogFormVisible = false;
          this.$message.success("操作成功");
          this.getRoleList();
        }
      });
    },
    deleteRole(id) {
      this.$confirm("此操作将永久删除该角色, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(async () => {
          delRole(id);
          this.getRoleList();
          this.$message({
            type: "success",
            message: "删除成功!"
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    async showDialog(data) {
      if (data) {
        this.title = "编辑角色";
        const res = await getRoleDetail(data);
        this.addform = res;
      } else {
        this.title = "添加角色";
      }
      this.dialogFormVisible = true;
    },
    async getCompanyInfo() {
      const res = await companyInfo(this.$store.getters.companyId);
      this.companyInfo = res;
    },
    getSelected(data) {
      console.log(data);
      console.log(this.selectedRole);
    },
    async showSetDialog(id) {
      const res = await getPermissionList();
      this.treeData = tranListToTreeData(res, "0");
      const { permIds } = await getRoleDetail(id)
      this.setId = id
      this.checkedNode = permIds
      console.log(this.checkedNode);
      this.setDialogFormVisible = true;
    },
    handleCheckChange() {
      console.log(this.$refs.treeRef.getCheckedKeys());
      // this.$refs.treeRef.getCheckedNodes
    },
    async postSetRole() {
      console.log({ id: this.setId, permIds: this.$refs.treeRef.getCheckedKeys() });
      await setRole({ id: this.setId, permIds: this.$refs.treeRef.getCheckedKeys() })
      this.$message.success('分配权限成功')
      this.setDialogFormVisible = false
      this.getRoleList()
    }
  }
};
</script>

<style>
.el-alert {
  margin-bottom: 20px;
}
.el-button {
  margin-bottom: 10px;
}
.el-card {
  padding: 20px !important;
}
.el-tabs__content .el-input__inner,
textarea {
  background-color: #f5f7fb;
}
</style>
